/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ifpe.helper;

import ifpe.command.Command;
import ifpe.command.CommandNotFoundException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author Sostenes
 */
public class RequestHelper {

    private Properties cmdProperties = new Properties();
    private static RequestHelper instance;

    public static synchronized RequestHelper getInstance(){
        if (instance == null) {
            instance = new RequestHelper();
        }
        return instance;
    }

    private RequestHelper() {
        try {
            cmdProperties.load(RequestHelper.class.getClassLoader().getResourceAsStream("ifpe/config/command.properties"));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    public Command getCommand(String command) {
        Command cmd = null;
        String commandClassName = cmdProperties.getProperty(command);

        try {
            cmd = (Command) Class.forName(commandClassName).newInstance();
        } catch (ClassNotFoundException ex) {
            throw new CommandNotFoundException(ex);
        } catch (InstantiationException ex) {
            throw new CommandNotFoundException(ex);
        } catch (IllegalAccessException ex) {
            throw new CommandNotFoundException(ex);
        }
        return cmd;
    }
}
